package com.example.demo.utils;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @Author Conn
 * @Date 2018/10/15
 */

@Service
@PropertySource(value = "classpath:config/elasticsearch.properties")
public class ElasticSearchUtils{
  private static final Logger logger = LoggerFactory.getLogger(ElasticSearchUtils.class);

  @Value("${es.hostName}")
  private String hostName;

  @Value("${es.transport}")
  private Integer transport;

  @Value("${es.cluster.name}")
  private String clusterName;

  public TransportClient transportClient() {
    logger.info("ElasticSearch初始化开始");

    TransportClient transportClient = null;

    try {
      TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(hostName), Integer.valueOf(transport));

      //配置信息
      Settings es = Settings.builder().put("cluster.name", clusterName).build();

      //配置信息Settings自定义
      transportClient = new PreBuiltTransportClient(es);

      transportClient.addTransportAddress(transportAddress);
    } catch (UnknownHostException e) {
      logger.error("ES创建错误", e);
    }
    return transportClient;
  }
}
